package com.ruoyi.puyu.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.puyu.domain.PuyuIntegral;
import com.ruoyi.puyu.domain.PuyuMemberIntegral;
import com.ruoyi.puyu.mapper.PuyuIntegralMapper;
import com.ruoyi.puyu.mapper.PuyuMemberIntegralMapper;
import com.ruoyi.puyu.service.IPuyuIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 积分明细Service业务层处理
 * 
 * @author wusongwen
 * @date 2024-12-18
 */
@Service
public class PuyuIntegralServiceImpl implements IPuyuIntegralService 
{
    @Autowired
    private PuyuIntegralMapper puyuIntegralMapper;
    @Autowired
    private PuyuMemberIntegralMapper puyuMemberIntegralMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询积分明细
     * 
     * @param integralId 积分明细主键
     * @return 积分明细
     */
    @Override
    public PuyuIntegral selectPuyuIntegralByIntegralId(Long integralId)
    {
        return puyuIntegralMapper.selectPuyuIntegralByIntegralId(integralId);
    }

    /**
     * 查询积分明细列表
     * 
     * @param puyuIntegral 积分明细
     * @return 积分明细
     */
    @Override
    public List<PuyuIntegral> selectPuyuIntegralList(PuyuIntegral puyuIntegral)
    {
        return puyuIntegralMapper.selectPuyuIntegralList(puyuIntegral);
    }

    /**
     * 新增积分明细
     * 
     * @param puyuIntegral 积分明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPuyuIntegral(PuyuIntegral puyuIntegral)
    {
        PuyuMemberIntegral puyuMemberIntegral = puyuMemberIntegralMapper.selectPuyuMemberIntegralByMemberId(puyuIntegral.getMemberId());
        puyuMemberIntegral.setIntegralLast(puyuIntegral.getIntegral());
        Long integralCalculate = puyuMemberIntegral.getIntegralCalculate() + puyuIntegral.getIntegral();
        puyuMemberIntegral.setIntegralCalculate(integralCalculate);
        Long integralTotal = puyuMemberIntegral.getIntegralBasic() + integralCalculate;
        puyuMemberIntegral.setIntegralTotal(integralTotal);
        puyuMemberIntegral.setUpdateBy(SecurityUtils.getUsername());
        puyuMemberIntegral.setUpdateTime(DateUtils.getNowDate());
        int result = puyuMemberIntegralMapper.updatePuyuMemberIntegral(puyuMemberIntegral);
        // 写入缓存
        if (result > 0) {
            String memberId = puyuMemberIntegral.getMemberId() + "_" + puyuMemberIntegral.getMemberName();
            redisCache.addMemberIntegral(memberId, puyuMemberIntegral.getIntegralTotal());
        }

        puyuIntegral.setCreateBy(SecurityUtils.getUsername());
        puyuIntegral.setCreateTime(DateUtils.getNowDate());
        return puyuIntegralMapper.insertPuyuIntegral(puyuIntegral);
    }

    /**
     * 修改积分明细
     * 
     * @param puyuIntegral 积分明细
     * @return 结果
     */
    @Override
    public int updatePuyuIntegral(PuyuIntegral puyuIntegral)
    {
        puyuIntegral.setUpdateTime(DateUtils.getNowDate());
        return puyuIntegralMapper.updatePuyuIntegral(puyuIntegral);
    }

    @Override
    public int updatePuyuIntegralPhone(PuyuIntegral puyuIntegral) {
        return puyuIntegralMapper.updatePuyuIntegralPhone(puyuIntegral);
    }

    @Override
    public int updatePuyuIntegralByMemberId(PuyuIntegral puyuIntegral) {
        return puyuIntegralMapper.updatePuyuIntegralByMemberId(puyuIntegral);
    }

    /**
     * 批量删除积分明细
     * 
     * @param integralIds 需要删除的积分明细主键
     * @return 结果
     */
    @Override
    public int deletePuyuIntegralByIntegralIds(Long[] integralIds)
    {
        return puyuIntegralMapper.deletePuyuIntegralByIntegralIds(integralIds);
    }

    /**
     * 删除积分明细信息
     * 
     * @param integralId 积分明细主键
     * @return 结果
     */
    @Override
    public int deletePuyuIntegralByIntegralId(Long integralId)
    {
        return puyuIntegralMapper.deletePuyuIntegralByIntegralId(integralId);
    }
}
